<template><div><p>本文档最新版为 <a href="https://learnku.com/docs/laravel/10.x" target="_blank" rel="noopener noreferrer">10.x</a>，旧版本可能放弃维护，推荐阅读最新版！</p>
<h2 id="laravel-的消息通知系统" tabindex="-1"><a class="header-anchor" href="#laravel-的消息通知系统"><span>Laravel 的消息通知系统</span></a></h2>
<ul>
<li><a href="#introduction">简介</a></li>
<li><a href="#creating-notifications">创建通知</a></li>
<li><a href="#sending-notifications">发送通知</a>
<ul>
<li><a href="#using-the-notifiable-trait">使用 Notifiable Trait</a></li>
<li><a href="#using-the-notification-facade">使用 Notification Facade</a></li>
<li><a href="#specifying-delivery-channels">指定发送频道</a></li>
<li><a href="#queueing-notifications">队列化通知</a></li>
<li><a href="#on-demand-notifications">按需通知</a></li>
</ul>
</li>
<li><a href="#mail-notifications">邮件通知</a>
<ul>
<li><a href="#formatting-mail-messages">格式化邮件消息</a></li>
<li><a href="#customizing-the-recipient">自定义接受者</a></li>
<li><a href="#customizing-the-subject">自定义主题</a></li>
<li><a href="#customizing-the-templates">自定义模板</a></li>
</ul>
</li>
<li><a href="#markdown-mail-notifications">Markdown 邮件通知</a>
<ul>
<li><a href="#generating-the-message">生成消息</a></li>
<li><a href="#writing-the-message">写消息</a></li>
<li><a href="#customizing-the-components">自定义组件</a></li>
</ul>
</li>
<li><a href="#database-notifications">数据库通知</a>
<ul>
<li><a href="#database-prerequisites">先决条件</a></li>
<li><a href="#formatting-database-notifications">格式化数据库通知</a></li>
<li><a href="#accessing-the-notifications">访问通知</a></li>
<li><a href="#marking-notifications-as-read">标为已读</a></li>
</ul>
</li>
<li><a href="#broadcast-notifications">广播通知</a>
<ul>
<li><a href="#broadcast-prerequisites">先决条件</a></li>
<li><a href="#formatting-broadcast-notifications">格式化广播通知</a></li>
<li><a href="#listening-for-notifications">监听通知</a></li>
</ul>
</li>
<li><a href="#sms-notifications">短信通知</a>
<ul>
<li><a href="#sms-prerequisites">先决条件</a></li>
<li><a href="#formatting-sms-notifications">格式化短信通知</a></li>
<li><a href="#customizing-the-from-number">自定义 <code v-pre>From</code> 号码</a></li>
<li><a href="#routing-sms-notifications">路由短信通知</a></li>
</ul>
</li>
<li><a href="#slack-notifications">Slack 通知</a>
<ul>
<li><a href="#slack-prerequisites">先决条件</a></li>
<li><a href="#formatting-slack-notifications">格式化 Slack 通知</a></li>
<li><a href="#slack-attachments">Slack Attachments</a></li>
<li><a href="#routing-slack-notifications">路由 Slack 通知</a></li>
</ul>
</li>
<li><a href="#notification-events">通知事件</a></li>
<li><a href="#custom-channels">自定义发送频道</a></li>
</ul>
<h2 id="简介" tabindex="-1"><a class="header-anchor" href="#简介"><span>简介</span></a></h2>
<p>除了 <a href="https://learnku.com/docs/laravel/5.5/mail" target="_blank" rel="noopener noreferrer">发送邮件</a>，Laravel 还支持通过多种频道发送通知，包括邮件、短信（通过 <a href="https://www.nexmo.com/" target="_blank" rel="noopener noreferrer">Nexmo</a>）以及 <a href="https://slack.com/" target="_blank" rel="noopener noreferrer">Slack</a> 。通知还能存到数据库，这样就能在网页界面上显示了。</p>
<p>通常情况下，通知应该是简短、有信息量的消息来通知用户你的应用发生了什么。举例来说，如果你在编写一个在线交易应用，你应该会通过邮件和短信频道来给用户发送一条 「账单已付」 的通知。</p>
<h2 id="创建通知" tabindex="-1"><a class="header-anchor" href="#创建通知"><span>创建通知</span></a></h2>
<p>Laravel 中一条通知就是一个类（通常存在 <code v-pre>app/Notifications</code> 文件夹里）。看不到的话不要担心，运行一下 <code v-pre>make:notification</code> 命令就能创建了：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">php artisan make<span class="token punctuation">:</span>notification InvoicePaid</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>这个命令会在 <code v-pre>app/Notifications</code> 目录下生成一个新的通知类。这个类包含 <code v-pre>via</code> 方法和几个消息构建方法（比如 <code v-pre>toMail</code> 或 <code v-pre>toDatabase</code>），它们会针对指定的渠道把通知转换过为对应的消息。</p>
<h2 id="发送通知" tabindex="-1"><a class="header-anchor" href="#发送通知"><span>发送通知</span></a></h2>
<h3 id="使用-notifiable-trait" tabindex="-1"><a class="header-anchor" href="#使用-notifiable-trait"><span>使用 Notifiable Trait</span></a></h3>
<p>通知可以通过两种方法发送： <code v-pre>Notifiable</code> trait 的 <code v-pre>notify</code> 方法或 <code v-pre>Notification</code> <a href="https://learnku.com/docs/laravel/5.5/facades" target="_blank" rel="noopener noreferrer">facade</a> 。首先，让我们探索使用 trait ：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">namespace</span> <span class="token package">App</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Notifications<span class="token punctuation">\</span>Notifiable</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Foundation<span class="token punctuation">\</span>Auth<span class="token punctuation">\</span>User</span> <span class="token keyword">as</span> Authenticatable<span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">User</span> <span class="token keyword">extends</span> <span class="token class-name">Authenticatable</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">use</span> <span class="token package">Notifiable</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>默认的 <code v-pre>App\User</code> 模型中使用了这个 trait，它包含着一个可以用来发通知的方法：<code v-pre>notify</code> 。 <code v-pre>notify</code> 方法需要一个通知实例做参数：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Notifications<span class="token punctuation">\</span>InvoicePaid</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$user</span><span class="token operator">-></span><span class="token function">notify</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">InvoicePaid</span><span class="token punctuation">(</span><span class="token variable">$invoice</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p>{tip} 记住，你可以在任意模型中使用 <code v-pre>Illuminate\Notifications\Notifiable</code> trait，而不仅仅是在 <code v-pre>User</code> 模型中。</p>
</blockquote>
<h3 id="使用-notification-facade" tabindex="-1"><a class="header-anchor" href="#使用-notification-facade"><span>使用 Notification Facade</span></a></h3>
<p>另外，你可以通过 <code v-pre>Notification</code> <a href="https://learnku.com/docs/laravel/5.5/facades" target="_blank" rel="noopener noreferrer">facade</a> 来发送通知。它主要用在当你给多个可接收通知的实体发送通知的时候，比如给用户集合发通知。要用 facade 发送通知的话，要把可接收通知的实体和通知的实例传递给 <code v-pre>send</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Notification</span><span class="token operator">::</span><span class="token function">send</span><span class="token punctuation">(</span><span class="token variable">$users</span><span class="token punctuation">,</span> <span class="token keyword">new</span> <span class="token class-name">InvoicePaid</span><span class="token punctuation">(</span><span class="token variable">$invoice</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="指定发送频道" tabindex="-1"><a class="header-anchor" href="#指定发送频道"><span>指定发送频道</span></a></h3>
<p>每个通知类都有个 <code v-pre>via</code> 方法，它决定了通知在哪个频道上发送。开箱即用的通知频道有 <code v-pre>mail</code>, <code v-pre>database</code>, <code v-pre>broadcast</code>, <code v-pre>nexmo</code>, 和 <code v-pre>slack</code> 。</p>
<blockquote>
<p>{tip} 如果你想用其他的频道比如 Telegram 或者 Pusher ，可以去看下社区驱动的 <a href="http://laravel-notification-channels.com/" target="_blank" rel="noopener noreferrer">Laravel 通知频道网站</a> 。</p>
</blockquote>
<p><code v-pre>via</code> 方法受到一个 <code v-pre>$notifiable</code> 实例，它是接收通知的类实例。你可以用 <code v-pre>$notifiable</code> 来决定通知用哪个频道来发送：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 获取通知发送频道</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@param</span>  <span class="token class-name"><span class="token keyword">mixed</span></span>  <span class="token parameter">$notifiable</span></span>
<span class="line"> * <span class="token keyword">@return</span> <span class="token class-name"><span class="token keyword">array</span></span></span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">via</span><span class="token punctuation">(</span><span class="token variable">$notifiable</span><span class="token punctuation">)</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token variable">$notifiable</span><span class="token operator">-></span><span class="token property">prefers_sms</span> <span class="token operator">?</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'nexmo'</span><span class="token punctuation">]</span> <span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'mail'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'database'</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="队列化通知" tabindex="-1"><a class="header-anchor" href="#队列化通知"><span>队列化通知</span></a></h3>
<blockquote>
<p>{note} 在队列化通知前你需要配置队列，并 <a href="https://learnku.com/docs/laravel/5.5/queues" target="_blank" rel="noopener noreferrer">运行队列处理器</a>。</p>
</blockquote>
<p>发送通知可能会花很长时间，尤其是发送频道需要调用外部 API 的时候。要加速应用响应的话，可以通过添加 <code v-pre>ShouldQueue</code> 接口和 <code v-pre>Queueable</code> trait 把通知加入队列。它们两个在使用 <code v-pre>make:notification</code> 命令来生成通知文件的时候就已经被导入了，所以你只需要添加到你的通知类就行了：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">namespace</span> <span class="token package">App<span class="token punctuation">\</span>Notifications</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Bus<span class="token punctuation">\</span>Queueable</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Notifications<span class="token punctuation">\</span>Notification</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Contracts<span class="token punctuation">\</span>Queue<span class="token punctuation">\</span>ShouldQueue</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">InvoicePaid</span> <span class="token keyword">extends</span> <span class="token class-name">Notification</span> <span class="token keyword">implements</span> <span class="token class-name">ShouldQueue</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">use</span> <span class="token package">Queueable</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// ...</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>一旦加入 <code v-pre>ShouldQueue</code> 接口，你就能像平常那样发送通知了。Laravel 会检测 <code v-pre>ShouldQueue</code> 接口并自动将通知的发送放入队列中。</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$user</span><span class="token operator">-></span><span class="token function">notify</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">InvoicePaid</span><span class="token punctuation">(</span><span class="token variable">$invoice</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>如果你想延迟发送，你可以通过 <code v-pre>delay</code> 方法来链式操作你的通知实例：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$when</span> <span class="token operator">=</span> <span class="token class-name static-context">Carbon</span><span class="token operator">::</span><span class="token function">now</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">addMinutes</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$user</span><span class="token operator">-></span><span class="token function">notify</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">InvoicePaid</span><span class="token punctuation">(</span><span class="token variable">$invoice</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">delay</span><span class="token punctuation">(</span><span class="token variable">$when</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="按需通知" tabindex="-1"><a class="header-anchor" href="#按需通知"><span>按需通知</span></a></h3>
<p>有时候你可能需要将通知发送给某个不是以”用户”身份存储在你的应用中的人。使用<code v-pre>Notification::route</code> 方法，你可以在发送通知之前指定 ad-hoc 通知路由信息：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Notification</span><span class="token operator">::</span><span class="token function">route</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'mail'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'taylor@laravel.com'</span><span class="token punctuation">)</span></span>
<span class="line">            <span class="token operator">-></span><span class="token function">route</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'nexmo'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'5555555555'</span><span class="token punctuation">)</span></span>
<span class="line">            <span class="token operator">-></span><span class="token function">notify</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">InvoicePaid</span><span class="token punctuation">(</span><span class="token variable">$invoice</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="邮件通知" tabindex="-1"><a class="header-anchor" href="#邮件通知"><span>邮件通知</span></a></h2>
<h3 id="格式化邮件消息" tabindex="-1"><a class="header-anchor" href="#格式化邮件消息"><span>格式化邮件消息</span></a></h3>
<p>如果一条通知支持以邮件发送，你应该在通知类里定义一个 <code v-pre>toMail</code> 方法。这个方法将收到一个 <code v-pre>$notifiable</code> 实体并返回一个 <code v-pre>Illuminate\Notifications\Messages\MailMessage</code> 实例。邮件消息可以包含多行文本也可以是引导链接。我们来看一个 <code v-pre>toMail</code> 方法的例子：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 获取通知的邮件展示方式</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@param</span>  <span class="token class-name"><span class="token keyword">mixed</span></span>  <span class="token parameter">$notifiable</span></span>
<span class="line"> * <span class="token keyword">@return</span> <span class="token class-name"><span class="token punctuation">\</span>Illuminate<span class="token punctuation">\</span>Notifications<span class="token punctuation">\</span>Messages<span class="token punctuation">\</span>MailMessage</span></span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">toMail</span><span class="token punctuation">(</span><span class="token variable">$notifiable</span><span class="token punctuation">)</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$url</span> <span class="token operator">=</span> <span class="token function">url</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/invoice/'</span><span class="token operator">.</span><span class="token variable">$this</span><span class="token operator">-></span><span class="token property">invoice</span><span class="token operator">-></span><span class="token property">id</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token keyword">return</span> <span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">MailMessage</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">line</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'One of your invoices has been paid!'</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">action</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'View Invoice'</span><span class="token punctuation">,</span> <span class="token variable">$url</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">line</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Thank you for using our application!'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p>{tip} 注意我们在方法中用了 <code v-pre>$this-&gt;invoice-&gt;id</code> ，其实你可以传递应用所需要的任何数据来传递给通知的构造器。</p>
</blockquote>
<p>在这个例子中，我们注册了一行文本，引导链接 ，然后又是一行文本。 <code v-pre>MailMessage</code> 提供的这些方法简化了对小的事务性的邮件进行格式化操作。邮件频道将会把这些消息组件转换成漂亮的响应式的 HTML 邮件模板并附上文本。下面是个 <code v-pre>mail</code> 频道生成的邮件示例：</p>
<p><img src="https://laravel.com/assets/img/notification-example.png" alt="notification-example.png"></p>
<blockquote>
<p>{tip} 发送邮件通知前，请在 <code v-pre>config/app.php</code> 中设置 <code v-pre>name</code> 值。 这将会在邮件通知消息的头部和尾部中被使用。</p>
</blockquote>
<h4 id="其他通知格式选项" tabindex="-1"><a class="header-anchor" href="#其他通知格式选项"><span>其他通知格式选项</span></a></h4>
<p>你可以使用 <code v-pre>view</code> 方法来指定一个应用于渲染通知电子邮件的自定义模板，而不是在通知类中定义文本的「模板」：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 获取通知的邮件展示方式</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@param</span>  <span class="token class-name"><span class="token keyword">mixed</span></span>  <span class="token parameter">$notifiable</span></span>
<span class="line"> * <span class="token keyword">@return</span> <span class="token class-name"><span class="token punctuation">\</span>Illuminate<span class="token punctuation">\</span>Notifications<span class="token punctuation">\</span>Messages<span class="token punctuation">\</span>MailMessage</span></span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">toMail</span><span class="token punctuation">(</span><span class="token variable">$notifiable</span><span class="token punctuation">)</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">MailMessage</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">view</span><span class="token punctuation">(</span></span>
<span class="line">        <span class="token string single-quoted-string">'emails.name'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'invoice'</span> <span class="token operator">=></span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token property">invoice</span><span class="token punctuation">]</span></span>
<span class="line">    <span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>另外，你可以从 <code v-pre>toMail</code> 方法中返回一个 <a href="https://learnku.com/docs/laravel/5.5/mail" target="_blank" rel="noopener noreferrer">mailable 对象</a> ：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Mail<span class="token punctuation">\</span>InvoicePaid</span> <span class="token keyword">as</span> Mailable<span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 获取通知的邮件展示方式</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@param</span>  <span class="token class-name"><span class="token keyword">mixed</span></span>  <span class="token parameter">$notifiable</span></span>
<span class="line"> * <span class="token keyword">@return</span> <span class="token class-name">Mailable</span></span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">toMail</span><span class="token punctuation">(</span><span class="token variable">$notifiable</span><span class="token punctuation">)</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">Mailable</span><span class="token punctuation">(</span><span class="token variable">$this</span><span class="token operator">-></span><span class="token property">invoice</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">to</span><span class="token punctuation">(</span><span class="token variable">$this</span><span class="token operator">-></span><span class="token property">user</span><span class="token operator">-></span><span class="token property">email</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="错误消息" tabindex="-1"><a class="header-anchor" href="#错误消息"><span>错误消息</span></a></h4>
<p>有些通知是给用户提示错误，比如账单支付失败的提示。你可以通过调用 <code v-pre>error</code> 方法来指定这条邮件消息被当做一个错误提示。当邮件消息使用了 <code v-pre>error</code> 方法后，引导链接按钮会变成红色而非蓝色：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 获取通知的邮件展示方式</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@param</span>  <span class="token class-name"><span class="token keyword">mixed</span></span>  <span class="token parameter">$notifiable</span></span>
<span class="line"> * <span class="token keyword">@return</span> <span class="token class-name"><span class="token punctuation">\</span>Illuminate<span class="token punctuation">\</span>Notifications<span class="token punctuation">\</span>Messages<span class="token punctuation">\</span>MailMessage</span></span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">toMail</span><span class="token punctuation">(</span><span class="token variable">$notifiable</span><span class="token punctuation">)</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">   <span class="token keyword">return</span> <span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">MailMessage</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">error</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">subject</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Notification Subject'</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">line</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'...'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="自定义接收者" tabindex="-1"><a class="header-anchor" href="#自定义接收者"><span>自定义接收者</span></a></h3>
<p>当通过 <code v-pre>mail</code> 频道来发送通知的时候，通知系统将会自动寻找你的 notifiable 实体中的 <code v-pre>email</code> 属性。你可以通过在实体中定义 <code v-pre>routeNotificationForMail</code> 方法来自定义邮件地址。</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">namespace</span> <span class="token package">App</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Notifications<span class="token punctuation">\</span>Notifiable</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Foundation<span class="token punctuation">\</span>Auth<span class="token punctuation">\</span>User</span> <span class="token keyword">as</span> Authenticatable<span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">User</span> <span class="token keyword">extends</span> <span class="token class-name">Authenticatable</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">use</span> <span class="token package">Notifiable</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * 邮件频道的路由</span>
<span class="line">     *</span>
<span class="line">     * <span class="token keyword">@return</span> <span class="token class-name"><span class="token keyword">string</span></span></span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">routeNotificationForMail</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token property">email_address</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="自定义主题" tabindex="-1"><a class="header-anchor" href="#自定义主题"><span>自定义主题</span></a></h3>
<p>默认情况下，邮件主题是格式化成了标题格式的通知类的类名。所以如果你对通知类名为 <code v-pre>InvoicePaid</code> ，邮件主题将会是 <code v-pre>Invoice Paid</code> 。如果你想显式指定消息的主题，你可以在构建消息时调用 <code v-pre>subject</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 获取通知的邮件展示方式</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@param</span>  <span class="token class-name"><span class="token keyword">mixed</span></span>  <span class="token parameter">$notifiable</span></span>
<span class="line"> * <span class="token keyword">@return</span> <span class="token class-name"><span class="token punctuation">\</span>Illuminate<span class="token punctuation">\</span>Notifications<span class="token punctuation">\</span>Messages<span class="token punctuation">\</span>MailMessage</span></span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">toMail</span><span class="token punctuation">(</span><span class="token variable">$notifiable</span><span class="token punctuation">)</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">MailMessage</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">subject</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Notification Subject'</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">line</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'...'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="自定义模板" tabindex="-1"><a class="header-anchor" href="#自定义模板"><span>自定义模板</span></a></h3>
<p>你可以通过发布通知包的资源来修改 HTML 模板和纯文本模板。运行这个命令后，邮件通知模板就被放在了 <code v-pre>resources/views/vendor/notifications</code> 文件夹下：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">php artisan vendor<span class="token punctuation">:</span>publish <span class="token operator">--</span>tag<span class="token operator">=</span>laravel<span class="token operator">-</span>notifications</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h2 id="markdown-邮件通知" tabindex="-1"><a class="header-anchor" href="#markdown-邮件通知"><span>Markdown 邮件通知</span></a></h2>
<p>Markdown 邮件通知可让您利用邮件通知的预先构建的模板，同时给予您更多自由地撰写更长的自定义邮件。 由于消息是用 Markdown 编写的， Laravel 能够为消息呈现漂亮，响应的 HTML 模板，同时还自动生成纯文本对应。</p>
<h3 id="生成消息" tabindex="-1"><a class="header-anchor" href="#生成消息"><span>生成消息</span></a></h3>
<p>要使用相应的 Markdown 模板生成通知，您可以使用 <code v-pre>make：notification</code> Artisan命令的 <code v-pre>--markdown</code> 选项：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">php artisan make<span class="token punctuation">:</span>notification InvoicePaid <span class="token operator">--</span>markdown<span class="token operator">=</span>mail<span class="token operator">.</span>invoice<span class="token operator">.</span>paid</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>与所有其他邮件通知一样，使用 Markdown 模板的通知应在其通知类上定义一个 <code v-pre>toMail</code> 方法。 但是，不使用 <code v-pre>line</code> 和 <code v-pre>action</code> 方法来构造通知，而是使用 <code v-pre>markdown</code> 方法来指定应该使用的 Markdown 模板的名称：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 获取通知的邮件展示方式</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@param</span>  <span class="token class-name"><span class="token keyword">mixed</span></span>  <span class="token parameter">$notifiable</span></span>
<span class="line"> * <span class="token keyword">@return</span> <span class="token class-name"><span class="token punctuation">\</span>Illuminate<span class="token punctuation">\</span>Notifications<span class="token punctuation">\</span>Messages<span class="token punctuation">\</span>MailMessage</span></span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">toMail</span><span class="token punctuation">(</span><span class="token variable">$notifiable</span><span class="token punctuation">)</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$url</span> <span class="token operator">=</span> <span class="token function">url</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/invoice/'</span><span class="token operator">.</span><span class="token variable">$this</span><span class="token operator">-></span><span class="token property">invoice</span><span class="token operator">-></span><span class="token property">id</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token keyword">return</span> <span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">MailMessage</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">subject</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Invoice Paid'</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">markdown</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'mail.invoice.paid'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'url'</span> <span class="token operator">=></span> <span class="token variable">$url</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="写消息" tabindex="-1"><a class="header-anchor" href="#写消息"><span>写消息</span></a></h3>
<p>Markdown 邮件通知使用 Blade 组件和Markdown语法的组合，允许您轻松构建通知，同时利用Laravel的预制通知组件：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">@<span class="token function">component</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'mail::message'</span><span class="token punctuation">)</span></span>
<span class="line"><span class="token comment"># Invoice Paid</span></span>
<span class="line"></span>
<span class="line">Your invoice has been paid<span class="token operator">!</span></span>
<span class="line"></span>
<span class="line">@<span class="token function">component</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'mail::button'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'url'</span> <span class="token operator">=></span> <span class="token variable">$url</span><span class="token punctuation">]</span><span class="token punctuation">)</span></span>
<span class="line">View Invoice</span>
<span class="line">@endcomponent</span>
<span class="line"></span>
<span class="line">Thanks<span class="token punctuation">,</span><span class="token operator">&lt;</span>br<span class="token operator">></span></span>
<span class="line"><span class="token punctuation">{</span><span class="token punctuation">{</span> <span class="token function">config</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'app.name'</span><span class="token punctuation">)</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></span>
<span class="line">@endcomponent</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="按钮组件" tabindex="-1"><a class="header-anchor" href="#按钮组件"><span>按钮组件</span></a></h4>
<p>按钮组件渲染了一个居中的链接按钮。组件有两个参数， <code v-pre>url</code> 和一个可选的参数 <code v-pre>color</code> 。支持的颜色有 <code v-pre>blue</code> ， <code v-pre>green</code> ， 和 <code v-pre>red</code> 。你可以根据你的需要向通知中添加任意数量的按钮组件：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">@<span class="token function">component</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'mail::button'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'url'</span> <span class="token operator">=></span> <span class="token variable">$url</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'color'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'green'</span><span class="token punctuation">]</span><span class="token punctuation">)</span></span>
<span class="line">View Invoice</span>
<span class="line">@endcomponent</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="面板组件" tabindex="-1"><a class="header-anchor" href="#面板组件"><span>面板组件</span></a></h4>
<p>面板部件在面板中呈现给定文本块，该面板具有与通知的其余部分稍微不同的背景颜色。 这将会允许你将给定的文本块引起注意：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">@<span class="token function">component</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'mail::panel'</span><span class="token punctuation">)</span></span>
<span class="line">This is the panel content<span class="token operator">.</span></span>
<span class="line">@endcomponent</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="表格组件" tabindex="-1"><a class="header-anchor" href="#表格组件"><span>表格组件</span></a></h4>
<p>表格组件允许你将一个 Markdown 表格转化成一个 HTML 表格。表格组件接受 Markdown 表格作为内容。支持使用默认的 Markdown 表格对齐语法使表列对齐：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">@<span class="token function">component</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'mail::table'</span><span class="token punctuation">)</span></span>
<span class="line"><span class="token operator">|</span> <span class="token class-name">Laravel</span>       <span class="token operator">|</span> <span class="token class-name">Table</span>         <span class="token operator">|</span> <span class="token class-name">Example</span>  <span class="token operator">|</span></span>
<span class="line"><span class="token operator">|</span> <span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">-</span> <span class="token operator">|</span><span class="token punctuation">:</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">-</span><span class="token punctuation">:</span><span class="token operator">|</span> <span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token punctuation">:</span><span class="token operator">|</span></span>
<span class="line"><span class="token operator">|</span> <span class="token class-name">Col</span> <span class="token number">2</span> <span class="token class-name">is</span>      <span class="token operator">|</span> <span class="token class-name">Centered</span>      <span class="token operator">|</span> <span class="token variable">$10</span>      <span class="token operator">|</span></span>
<span class="line"><span class="token operator">|</span> <span class="token class-name">Col</span> <span class="token number">3</span> <span class="token class-name">is</span>      <span class="token operator">|</span> <span class="token class-name">Right</span><span class="token operator">-</span><span class="token class-name">Aligned</span> <span class="token operator">|</span> <span class="token variable">$20</span>      <span class="token operator">|</span></span>
<span class="line">@endcomponent</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="自定义组件" tabindex="-1"><a class="header-anchor" href="#自定义组件"><span>自定义组件</span></a></h3>
<p>您可以将所有 Markdown 通知组件导出到您自己的应用程序中进行自定义。 要导出组件，请使用 <code v-pre>vendor：publish</code> Artisan命令来发布 <code v-pre>laravel-mail</code> 标签：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">php artisan vendor<span class="token punctuation">:</span>publish <span class="token operator">--</span>tag<span class="token operator">=</span>laravel<span class="token operator">-</span>mail</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>此命令将 Markdown 邮件组件发布到 <code v-pre>resources/views/vendor/mail</code> 目录。 <code v-pre>mail</code> 目录将包含一个 <code v-pre>html</code> 和一个 <code v-pre>markdown</code> 目录，每个目录包含它们各自可用组件的展示方式。 您可以随意自定义这些组件。</p>
<h4 id="自定义css" tabindex="-1"><a class="header-anchor" href="#自定义css"><span>自定义CSS</span></a></h4>
<p>导出组件后， <code v-pre>resources/views/vendor/mail/html/themes</code> 目录将包含一个 <code v-pre>default.css</code> 文件。 您可以在此文件中自定义 CSS ，并且您的样式将自动内嵌在 Markdown 通知的HTML展示中。</p>
<blockquote>
<p>{tip} 如果您想为 Markdown 组件构建一个全新的主题，只需在 <code v-pre>html/themes</code> 目录中写一个新的 CSS 文件，并更改 <code v-pre>mail</code> 配置文件中的 <code v-pre>theme</code> 选项。</p>
</blockquote>
<h2 id="数据库通知" tabindex="-1"><a class="header-anchor" href="#数据库通知"><span>数据库通知</span></a></h2>
<h3 id="先决条件" tabindex="-1"><a class="header-anchor" href="#先决条件"><span>先决条件</span></a></h3>
<p><code v-pre>database</code> 通知频道在一张数据表里存储通知信息。这张表包含了比如通知类型、JSON 格式数据等描述通知的信息。</p>
<p>你可以查询这张表的内容在应用界面上展示通知。但是在这之前，你需要先创建一个数据表来保存通知。你可以用 <code v-pre>notifications:table</code> 命令来生成迁移表：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">php artisan notifications<span class="token punctuation">:</span>table</span>
<span class="line"></span>
<span class="line">php artisan migrate</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="格式化数据库通知" tabindex="-1"><a class="header-anchor" href="#格式化数据库通知"><span>格式化数据库通知</span></a></h3>
<p>如果通知支持被存储到数据表中，你应该在通知类中定义一个 <code v-pre>toDatabase</code> 或 <code v-pre>toArray</code> 方法。这个方法接收 <code v-pre>$notifiable</code> 实体参数并返回一个普通的 PHP 数组。这个返回的数组将被转成 JSON 格式并存储到通知数据表的 <code v-pre>data</code> 列。我们来看一个 <code v-pre>toArray</code> 的例子：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 获取通知的数组展示方式</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@param</span>  <span class="token class-name"><span class="token keyword">mixed</span></span>  <span class="token parameter">$notifiable</span></span>
<span class="line"> * <span class="token keyword">@return</span> <span class="token class-name"><span class="token keyword">array</span></span></span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">toArray</span><span class="token punctuation">(</span><span class="token variable">$notifiable</span><span class="token punctuation">)</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'invoice_id'</span> <span class="token operator">=></span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token property">invoice</span><span class="token operator">-></span><span class="token property">id</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'amount'</span> <span class="token operator">=></span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token property">invoice</span><span class="token operator">-></span><span class="token property">amount</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="todatabase-vs-toarray" tabindex="-1"><a class="header-anchor" href="#todatabase-vs-toarray"><span><code v-pre>toDatabase</code> Vs <code v-pre>toArray</code></span></a></h4>
<p><code v-pre>toArray</code> 方法在 <code v-pre>broadcast</code> 频道也用到了，它用来决定广播给 JavaScript 客户端的数据。如果你想在 <code v-pre>database</code> 和 <code v-pre>broadcast</code> 频道中采用两种不同的数组展示方式，你应该定义 <code v-pre>toDatabase</code> 方法而非 <code v-pre>toArray</code> 方法。</p>
<h3 id="访问通知" tabindex="-1"><a class="header-anchor" href="#访问通知"><span>访问通知</span></a></h3>
<p>一旦通知被存到数据库中，你需要一种方便的方式来从通知实体中访问它们。 <code v-pre>Illuminate\Notifications\Notifiable</code> trait 包含一个可以返回这个实体所有通知的 <code v-pre>notifications</code> Eloquent 关联。要获取这些通知，你可以像用其他 Eloquent 关联一样来使用这个方法。默认情况下，通知将会以 <code v-pre>created_at</code> 时间戳来排序：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$user</span> <span class="token operator">=</span> <span class="token class-name class-name-fully-qualified static-context">App<span class="token punctuation">\</span>User</span><span class="token operator">::</span><span class="token function">find</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">foreach</span> <span class="token punctuation">(</span><span class="token variable">$user</span><span class="token operator">-></span><span class="token property">notifications</span> <span class="token keyword">as</span> <span class="token variable">$notification</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">echo</span> <span class="token variable">$notification</span><span class="token operator">-></span><span class="token property">type</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果你只想检索未读通知，你可以使用 <code v-pre>unreadNotifications</code> 关联。检索出来的通知也是以 <code v-pre>created_at</code> 时间戳来排序的：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$user</span> <span class="token operator">=</span> <span class="token class-name class-name-fully-qualified static-context">App<span class="token punctuation">\</span>User</span><span class="token operator">::</span><span class="token function">find</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">foreach</span> <span class="token punctuation">(</span><span class="token variable">$user</span><span class="token operator">-></span><span class="token property">unreadNotifications</span> <span class="token keyword">as</span> <span class="token variable">$notification</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">echo</span> <span class="token variable">$notification</span><span class="token operator">-></span><span class="token property">type</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p>{tip} 要从 JavaScript 客户端来访问通知的话，你应该定义一个通知控制器来给可通知的实体返回通知，比如给当前用户返回通知。然后你就可以在 JavaScript 客户端来发起对应 URI 的 HTTP 请求了。</p>
</blockquote>
<h3 id="标为已读" tabindex="-1"><a class="header-anchor" href="#标为已读"><span>标为已读</span></a></h3>
<p>通常情况下，当用户查看了通知时，你就希望把通知标为已读。<code v-pre>Illuminate\Notifications\Notifiable</code> trait 提供了一个 <code v-pre>markAsRead</code> 方法，它能在对应的数据库记录里更新 <code v-pre>read_at</code> 列：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$user</span> <span class="token operator">=</span> <span class="token class-name class-name-fully-qualified static-context">App<span class="token punctuation">\</span>User</span><span class="token operator">::</span><span class="token function">find</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">foreach</span> <span class="token punctuation">(</span><span class="token variable">$user</span><span class="token operator">-></span><span class="token property">unreadNotifications</span> <span class="token keyword">as</span> <span class="token variable">$notification</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$notification</span><span class="token operator">-></span><span class="token function">markAsRead</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>你可以使用 <code v-pre>markAsRead</code> 方法直接操作一个通知集合，而不是一条条遍历每个通知：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$user</span><span class="token operator">-></span><span class="token property">unreadNotifications</span><span class="token operator">-></span><span class="token function">markAsRead</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>你可以用批量更新的方式来把所有通知标为已读，而不用在数据库里检索：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$user</span> <span class="token operator">=</span> <span class="token class-name class-name-fully-qualified static-context">App<span class="token punctuation">\</span>User</span><span class="token operator">::</span><span class="token function">find</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$user</span><span class="token operator">-></span><span class="token function">unreadNotifications</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">update</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'read_at'</span> <span class="token operator">=></span> <span class="token class-name static-context">Carbon</span><span class="token operator">::</span><span class="token function">now</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>当然，你可以通过 <code v-pre>delete</code> 通知来把它们从数据库删除：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$user</span><span class="token operator">-></span><span class="token function">notifications</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">delete</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h2 id="广播通知" tabindex="-1"><a class="header-anchor" href="#广播通知"><span>广播通知</span></a></h2>
<h3 id="先决条件-1" tabindex="-1"><a class="header-anchor" href="#先决条件-1"><span>先决条件</span></a></h3>
<p>在广播通知前，你应该配置并熟悉 Laravel <a href="https://learnku.com/docs/laravel/5.5/broadcasting" target="_blank" rel="noopener noreferrer">事件广播</a> 服务。事件广播提供了一种 JavaScript 客户端响应服务端 Laravel 事件的机制。</p>
<h3 id="格式化广播通知" tabindex="-1"><a class="header-anchor" href="#格式化广播通知"><span>格式化广播通知</span></a></h3>
<p><code v-pre>broadcast</code> 频道使用 Laravel <a href="https://learnku.com/docs/laravel/5.5/broadcasting" target="_blank" rel="noopener noreferrer">事件广播</a> 服务来广播通知，它使得 JavaScript 客户端可以实时捕捉通知。如果一条通知支持广播，你应该在通知类里定义一个 <code v-pre>toBroadcast</code> 或 <code v-pre>toArray</code> 方法。这个方法将收到一个 <code v-pre>$notifiable</code> 实体并返回一个普通的 PHP 数组。返回的数组会被编码成 JSON 格式并广播给你的 JavaScript 客户端。我们来看个 <code v-pre>toArray</code> 方法的例子：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Notifications<span class="token punctuation">\</span>Messages<span class="token punctuation">\</span>BroadcastMessage</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 获取通知的数组展示方式</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@param</span>  <span class="token class-name"><span class="token keyword">mixed</span></span>  <span class="token parameter">$notifiable</span></span>
<span class="line"> * <span class="token keyword">@return</span> <span class="token class-name">BroadcastMessage</span></span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">toBroadcast</span><span class="token punctuation">(</span><span class="token variable">$notifiable</span><span class="token punctuation">)</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name">BroadcastMessage</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'invoice_id'</span> <span class="token operator">=></span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token property">invoice</span><span class="token operator">-></span><span class="token property">id</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'amount'</span> <span class="token operator">=></span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token property">invoice</span><span class="token operator">-></span><span class="token property">amount</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="广播队列配置" tabindex="-1"><a class="header-anchor" href="#广播队列配置"><span>广播队列配置</span></a></h4>
<p>所有广播通知都排队等待广播。 如果要配置用于广播队列操作的队列连接或队列名称，你可以使用 <code v-pre>BroadcastMessage</code> 的 <code v-pre>onConnection</code> 和 <code v-pre>onQueue</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name">BroadcastMessage</span><span class="token punctuation">(</span><span class="token variable">$data</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">onConnection</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'sqs'</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">onQueue</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'broadcasts'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p>{tip} 除了你指定的数据外，广播通知也包含一个 <code v-pre>type</code> 字段，这个字段包含了通知类的类名。</p>
</blockquote>
<h3 id="监听通知" tabindex="-1"><a class="header-anchor" href="#监听通知"><span>监听通知</span></a></h3>
<p>通知将会在一个私有频道里进行广播，频道格式为 <code v-pre>{notifiable}.{id}</code>。所以，如果你给 ID 为 <code v-pre>1</code> 的 <code v-pre>App\User</code> 实例发送通知，这个通知就在 <code v-pre>App.User.1</code> 私有频道里被发送。当你使用 <a href="https://learnku.com/docs/laravel/5.5/broadcasting" target="_blank" rel="noopener noreferrer">Laravel Echo</a> 的时候，你可以很简单地使用 <code v-pre>notification</code> 辅助函数来监听一个频道的通知：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">Echo</span><span class="token operator">.</span><span class="token keyword">private</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'App.User.'</span> <span class="token operator">+</span> userId<span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">.</span><span class="token function">notification</span><span class="token punctuation">(</span><span class="token punctuation">(</span>notification<span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">{</span></span>
<span class="line">        console<span class="token operator">.</span><span class="token function">log</span><span class="token punctuation">(</span>notification<span class="token operator">.</span>type<span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="自定义通知通道" tabindex="-1"><a class="header-anchor" href="#自定义通知通道"><span>自定义通知通道</span></a></h4>
<p>如果您想自定义应通知实体接收其广播通知的渠道，您可以定义一个 <code v-pre>receiveBroadcastNotificationsOn</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">namespace</span> <span class="token package">App</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Notifications<span class="token punctuation">\</span>Notifiable</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Broadcasting<span class="token punctuation">\</span>PrivateChannel</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Foundation<span class="token punctuation">\</span>Auth<span class="token punctuation">\</span>User</span> <span class="token keyword">as</span> Authenticatable<span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">User</span> <span class="token keyword">extends</span> <span class="token class-name">Authenticatable</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">use</span> <span class="token package">Notifiable</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * 用户接收的通知广播</span>
<span class="line">     *</span>
<span class="line">     * <span class="token keyword">@return</span> <span class="token class-name"><span class="token keyword">array</span></span></span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">receivesBroadcastNotificationsOn</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token keyword">new</span> <span class="token class-name">PrivateChannel</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users.'</span><span class="token operator">.</span><span class="token variable">$this</span><span class="token operator">-></span><span class="token property">id</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="短信通知" tabindex="-1"><a class="header-anchor" href="#短信通知"><span>短信通知</span></a></h2>
<h3 id="先决条件-2" tabindex="-1"><a class="header-anchor" href="#先决条件-2"><span>先决条件</span></a></h3>
<p>在 Laravel 中发送短信通知是基于 <a href="https://www.nexmo.com/" target="_blank" rel="noopener noreferrer">Nexmo</a> 服务的。在通过 Nexmo 发送短信通知前，你需要安装 <code v-pre>nexmo/client</code> Composer 包并在 <code v-pre>config/services.php</code> 配置文件中添加几个配置选项。你可以复制下面的配置示例来开始使用：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'nexmo'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'key'</span> <span class="token operator">=></span> <span class="token function">env</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'NEXMO_KEY'</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'secret'</span> <span class="token operator">=></span> <span class="token function">env</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'NEXMO_SECRET'</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'sms_from'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'15556666666'</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p><code v-pre>sms_from</code> 选项是短信消息发送者的手机号码。你可以在 Nexmo 控制面板中生成一个手机号码。</p>
<h3 id="格式化短信通知" tabindex="-1"><a class="header-anchor" href="#格式化短信通知"><span>格式化短信通知</span></a></h3>
<p>如果通知支持以短信方式发送，你应该在通知类里定义一个 <code v-pre>toNexmo</code> 方法。这个方法将会收到一个 <code v-pre>$notifiable</code> 实体并返回一个 <code v-pre>Illuminate\Notifications\Messages\NexmoMessage</code> 实例：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 获取通知的 Nexmo / 短信展示方式</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@param</span>  <span class="token class-name"><span class="token keyword">mixed</span></span>  <span class="token parameter">$notifiable</span></span>
<span class="line"> * <span class="token keyword">@return</span> <span class="token class-name">NexmoMessage</span></span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">toNexmo</span><span class="token punctuation">(</span><span class="token variable">$notifiable</span><span class="token punctuation">)</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">NexmoMessage</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">content</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Your SMS message content'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="unicode-内容" tabindex="-1"><a class="header-anchor" href="#unicode-内容"><span>Unicode 内容</span></a></h4>
<p>如果您的 SMS 消息包含 unicode 字符，您应该在构建 <code v-pre>NexmoMessage</code> 实例时调用 <code v-pre>unicode</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 获取通知的 Nexmo / 短信展示方式</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@param</span>  <span class="token class-name"><span class="token keyword">mixed</span></span>  <span class="token parameter">$notifiable</span></span>
<span class="line"> * <span class="token keyword">@return</span> <span class="token class-name">NexmoMessage</span></span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">toNexmo</span><span class="token punctuation">(</span><span class="token variable">$notifiable</span><span class="token punctuation">)</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">NexmoMessage</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">content</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Your unicode message'</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">unicode</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="自定义-from-号码" tabindex="-1"><a class="header-anchor" href="#自定义-from-号码"><span>自定义 <code v-pre>From</code> 号码</span></a></h3>
<p>如果你想通过一个手机号来发送某些通知，而这个手机号不同于你的 <code v-pre>config/services.php</code> 配置文件中指定的话，你可以在 <code v-pre>NexmoMessage</code> 实例中使用 <code v-pre>from</code>：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 获取通知的 Nexmo / 短信展示方式</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@param</span>  <span class="token class-name"><span class="token keyword">mixed</span></span>  <span class="token parameter">$notifiable</span></span>
<span class="line"> * <span class="token keyword">@return</span> <span class="token class-name">NexmoMessage</span></span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">toNexmo</span><span class="token punctuation">(</span><span class="token variable">$notifiable</span><span class="token punctuation">)</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">NexmoMessage</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">content</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Your SMS message content'</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">from</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'15554443333'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="路由短信通知" tabindex="-1"><a class="header-anchor" href="#路由短信通知"><span>路由短信通知</span></a></h3>
<p>当通过 <code v-pre>nexmo</code> 频道来发送通知的时候，通知系统会自动寻找通知实体的 <code v-pre>phone_number</code> 属性。如果你想自定义通知被发送的手机号码，你要在通知实体里定义一个 <code v-pre>routeNotificationForNexmo</code> 方法。</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">namespace</span> <span class="token package">App</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Notifications<span class="token punctuation">\</span>Notifiable</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Foundation<span class="token punctuation">\</span>Auth<span class="token punctuation">\</span>User</span> <span class="token keyword">as</span> Authenticatable<span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">User</span> <span class="token keyword">extends</span> <span class="token class-name">Authenticatable</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">use</span> <span class="token package">Notifiable</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * Nexmo 频道的路由通知</span>
<span class="line">     *</span>
<span class="line">     * <span class="token keyword">@return</span> <span class="token class-name"><span class="token keyword">string</span></span></span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">routeNotificationForNexmo</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token property">phone</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="slack-通知" tabindex="-1"><a class="header-anchor" href="#slack-通知"><span>Slack 通知</span></a></h2>
<h3 id="先决条件-3" tabindex="-1"><a class="header-anchor" href="#先决条件-3"><span>先决条件</span></a></h3>
<p>通过 Slack 发送通知前，你必须通过 Composer 安装 Guzzle HTTP 库：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">composer <span class="token keyword">require</span> guzzlehttp<span class="token operator">/</span>guzzle</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>你还需要给 Slack 组配置 「Incoming Webhook」 。它将提供你使用 <a href="#routing-slack-notifications">路由 Slack 通知</a> 时用到的 URL。</p>
<h3 id="格式化-slack-通知" tabindex="-1"><a class="header-anchor" href="#格式化-slack-通知"><span>格式化 Slack 通知</span></a></h3>
<p>如果通知支持被当做 Slack 消息发送，你应该在通知类里定义一个 <code v-pre>toSlack</code> 方法。这个方法将收到一个 <code v-pre>$notifiable</code> 实体并且返回一个 <code v-pre>Illuminate\Notifications\Messages\SlackMessage</code> 实例。Slack 消息可以包含文本内容以及一个 「attachment」 用来格式化额外文本或者字段数组。我们来看个基本的 <code v-pre>toSlack</code> 例子：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 获取通知的 Slack 展示方式</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@param</span>  <span class="token class-name"><span class="token keyword">mixed</span></span>  <span class="token parameter">$notifiable</span></span>
<span class="line"> * <span class="token keyword">@return</span> <span class="token class-name">SlackMessage</span></span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">toSlack</span><span class="token punctuation">(</span><span class="token variable">$notifiable</span><span class="token punctuation">)</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">SlackMessage</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">content</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'One of your invoices has been paid!'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>这个例子中，我们只发送了一行文本给 Slack，这会创建类似下面的一条消息：</p>
<p><img src="https://laravel.com/assets/img/basic-slack-notification.png" alt="basic-slack-notification.png"></p>
<h4 id="自定义发件人和收件人" tabindex="-1"><a class="header-anchor" href="#自定义发件人和收件人"><span>自定义发件人和收件人</span></a></h4>
<p>您可以使用 <code v-pre>from</code> 和 <code v-pre>to</code> 方法来自定义发件人和收件人。 <code v-pre>from</code> 方法接受用户名和表情符号标识符，而 <code v-pre>to</code> 方法接受一个频道或用户名：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 获取通知的 Slack 展示方式</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@param</span>  <span class="token class-name"><span class="token keyword">mixed</span></span>  <span class="token parameter">$notifiable</span></span>
<span class="line"> * <span class="token keyword">@return</span> <span class="token class-name">SlackMessage</span></span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">toSlack</span><span class="token punctuation">(</span><span class="token variable">$notifiable</span><span class="token punctuation">)</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">SlackMessage</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">from</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Ghost'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">':ghost:'</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">to</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'#other'</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">content</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'This will be sent to #other'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>你也可以不使用表情符号，改为使用图片作为你的 logo：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 获取通知的 Slack 展示方式</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@param</span>  <span class="token class-name"><span class="token keyword">mixed</span></span>  <span class="token parameter">$notifiable</span></span>
<span class="line"> * <span class="token keyword">@return</span> <span class="token class-name">SlackMessage</span></span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">toSlack</span><span class="token punctuation">(</span><span class="token variable">$notifiable</span><span class="token punctuation">)</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">SlackMessage</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">from</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Laravel'</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">image</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'https://laravel.com/favicon.png'</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">content</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'This will display the Laravel logo next to the message'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="slack-附加项-attachments" tabindex="-1"><a class="header-anchor" href="#slack-附加项-attachments"><span>Slack 附加项 (Attachments)</span></a></h3>
<p>你可以给 Slack 消息添加 &quot;附加项&quot;。附加项提供了比简单文本消息更丰富的格式化选项。在这个例子中，我们将发送一条有关应用中异常的错误通知，它里面有个可以查看这个异常更多详情的链接：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 获取通知的 Slack 展示方式。</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@param</span>  <span class="token class-name"><span class="token keyword">mixed</span></span>  <span class="token parameter">$notifiable</span></span>
<span class="line"> * <span class="token keyword">@return</span> <span class="token class-name">SlackMessage</span></span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">toSlack</span><span class="token punctuation">(</span><span class="token variable">$notifiable</span><span class="token punctuation">)</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$url</span> <span class="token operator">=</span> <span class="token function">url</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/exceptions/'</span><span class="token operator">.</span><span class="token variable">$this</span><span class="token operator">-></span><span class="token property">exception</span><span class="token operator">-></span><span class="token property">id</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token keyword">return</span> <span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">SlackMessage</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">error</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">content</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Whoops! Something went wrong.'</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">attachment</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token variable">$attachment</span><span class="token punctuation">)</span> <span class="token keyword">use</span> <span class="token punctuation">(</span><span class="token variable">$url</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">                    <span class="token variable">$attachment</span><span class="token operator">-></span><span class="token function">title</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Exception: File Not Found'</span><span class="token punctuation">,</span> <span class="token variable">$url</span><span class="token punctuation">)</span></span>
<span class="line">                               <span class="token operator">-></span><span class="token function">content</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'File [background.jpg] was not found.'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">                <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>上面这个例子将会生成一条类似下面的 Slack 消息：</p>
<p><img src="https://laravel.com/assets/img/basic-slack-attachment.png" alt="basic-slack-attachment.png"></p>
<p>附加项也允许你指定一个应该被展示给用户的数据的数组。给定的数据将会以表格样式展示出来，这能方便阅读：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 获取通知的 Slack 展示方式</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@param</span>  <span class="token class-name"><span class="token keyword">mixed</span></span>  <span class="token parameter">$notifiable</span></span>
<span class="line"> * <span class="token keyword">@return</span> <span class="token class-name">SlackMessage</span></span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">toSlack</span><span class="token punctuation">(</span><span class="token variable">$notifiable</span><span class="token punctuation">)</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$url</span> <span class="token operator">=</span> <span class="token function">url</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/invoices/'</span><span class="token operator">.</span><span class="token variable">$this</span><span class="token operator">-></span><span class="token property">invoice</span><span class="token operator">-></span><span class="token property">id</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token keyword">return</span> <span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">SlackMessage</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">success</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">content</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'One of your invoices has been paid!'</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">attachment</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token variable">$attachment</span><span class="token punctuation">)</span> <span class="token keyword">use</span> <span class="token punctuation">(</span><span class="token variable">$url</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">                    <span class="token variable">$attachment</span><span class="token operator">-></span><span class="token function">title</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Invoice 1322'</span><span class="token punctuation">,</span> <span class="token variable">$url</span><span class="token punctuation">)</span></span>
<span class="line">                               <span class="token operator">-></span><span class="token function">fields</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">                                    <span class="token string single-quoted-string">'Title'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Server Expenses'</span><span class="token punctuation">,</span></span>
<span class="line">                                    <span class="token string single-quoted-string">'Amount'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'$1,234'</span><span class="token punctuation">,</span></span>
<span class="line">                                    <span class="token string single-quoted-string">'Via'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'American Express'</span><span class="token punctuation">,</span></span>
<span class="line">                                    <span class="token string single-quoted-string">'Was Overdue'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">':-1:'</span><span class="token punctuation">,</span></span>
<span class="line">                                <span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">                <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>上面的例子将会生成一条类似下面的 Slack 消息：</p>
<p><img src="https://laravel.com/assets/img/slack-fields-attachment.png" alt="slack-fields-attachment.png"></p>
<h4 id="markdown-附件内容" tabindex="-1"><a class="header-anchor" href="#markdown-附件内容"><span>Markdown 附件内容</span></a></h4>
<p>如果一些附件字段包含 Markdown ，您可以使用 <code v-pre>markdown</code> 方法指示 Slack 解析并将给定的附件字段显示为 Markdown 格式的文本：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 获取通知的 Slack 展示方式</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@param</span>  <span class="token class-name"><span class="token keyword">mixed</span></span>  <span class="token parameter">$notifiable</span></span>
<span class="line"> * <span class="token keyword">@return</span> <span class="token class-name">SlackMessage</span></span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">toSlack</span><span class="token punctuation">(</span><span class="token variable">$notifiable</span><span class="token punctuation">)</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$url</span> <span class="token operator">=</span> <span class="token function">url</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/exceptions/'</span><span class="token operator">.</span><span class="token variable">$this</span><span class="token operator">-></span><span class="token property">exception</span><span class="token operator">-></span><span class="token property">id</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token keyword">return</span> <span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">SlackMessage</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">error</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">content</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Whoops! Something went wrong.'</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">attachment</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token variable">$attachment</span><span class="token punctuation">)</span> <span class="token keyword">use</span> <span class="token punctuation">(</span><span class="token variable">$url</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">                    <span class="token variable">$attachment</span><span class="token operator">-></span><span class="token function">title</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Exception: File Not Found'</span><span class="token punctuation">,</span> <span class="token variable">$url</span><span class="token punctuation">)</span></span>
<span class="line">                               <span class="token operator">-></span><span class="token function">content</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'File [background.jpg] was **not found**.'</span><span class="token punctuation">)</span></span>
<span class="line">                               <span class="token operator">-></span><span class="token function">markdown</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'title'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'text'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">                <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="路由-slack-通知" tabindex="-1"><a class="header-anchor" href="#路由-slack-通知"><span>路由 Slack 通知</span></a></h3>
<p>要把 Slack 通知路由到正确的位置，你要在通知实体中定义 <code v-pre>routeNotificationForSlack</code> 方法。它返回通知要被发往的 URL 回调地址。URL 可以通过在 Slack 组里面添加 &quot;Incoming Webhook&quot; 服务来生成。</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">namespace</span> <span class="token package">App</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Notifications<span class="token punctuation">\</span>Notifiable</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Foundation<span class="token punctuation">\</span>Auth<span class="token punctuation">\</span>User</span> <span class="token keyword">as</span> Authenticatable<span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">User</span> <span class="token keyword">extends</span> <span class="token class-name">Authenticatable</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">use</span> <span class="token package">Notifiable</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * Slack 频道的通知路由</span>
<span class="line">     *</span>
<span class="line">     * <span class="token keyword">@return</span> <span class="token class-name"><span class="token keyword">string</span></span></span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">routeNotificationForSlack</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token property">slack_webhook_url</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="通知事件" tabindex="-1"><a class="header-anchor" href="#通知事件"><span>通知事件</span></a></h2>
<p>当通知发送后，通知系统就会触发 <code v-pre>Illuminate\Notifications\Events\NotificationSent</code> 事件。它包含了 「notifiable」 实体和通知实例本身。你应该在 <code v-pre>EventServiceProvider</code> 中注册监听器：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 应用中的事件监听映射</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@var</span> <span class="token class-name"><span class="token keyword">array</span></span></span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">protected</span> <span class="token variable">$listen</span> <span class="token operator">=</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'Illuminate\Notifications\Events\NotificationSent'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'App\Listeners\LogNotification'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p>{tip} 注册完监听器后，使用 <code v-pre>event:generate</code> Artisan 命令来快速生成监听器类。</p>
</blockquote>
<p>在事件监听器中，你可以访问事件中的 <code v-pre>notifiable</code>, <code v-pre>notification</code>, 和 <code v-pre>channel</code> 属性，来了解通知接收者和通知本身：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 处理事件</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@param</span>  <span class="token class-name">NotificationSent</span>  <span class="token parameter">$event</span></span>
<span class="line"> * <span class="token keyword">@return</span> <span class="token class-name"><span class="token keyword">void</span></span></span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">handle</span><span class="token punctuation">(</span><span class="token class-name type-declaration">NotificationSent</span> <span class="token variable">$event</span><span class="token punctuation">)</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token comment">// $event->channel</span></span>
<span class="line">    <span class="token comment">// $event->notifiable</span></span>
<span class="line">    <span class="token comment">// $event->notification</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="自定义频道" tabindex="-1"><a class="header-anchor" href="#自定义频道"><span>自定义频道</span></a></h2>
<p>Laravel 提供了开箱即用的通知频道，但是你可能会想编写自己的驱动来通过其他频道发送通知。Laravel 很容易实现。首先，定义一个包含 <code v-pre>send</code> 方法的类。这个方法应该收到两个参数：<code v-pre>$notifiable</code> 和 <code v-pre>$notification</code>:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">namespace</span> <span class="token package">App<span class="token punctuation">\</span>Channels</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Notifications<span class="token punctuation">\</span>Notification</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">VoiceChannel</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * 发送给定通知</span>
<span class="line">     *</span>
<span class="line">     * <span class="token keyword">@param</span>  <span class="token class-name"><span class="token keyword">mixed</span></span>  <span class="token parameter">$notifiable</span></span>
<span class="line">     * <span class="token keyword">@param</span>  <span class="token class-name"><span class="token punctuation">\</span>Illuminate<span class="token punctuation">\</span>Notifications<span class="token punctuation">\</span>Notification</span>  <span class="token parameter">$notification</span></span>
<span class="line">     * <span class="token keyword">@return</span> <span class="token class-name"><span class="token keyword">void</span></span></span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">send</span><span class="token punctuation">(</span><span class="token variable">$notifiable</span><span class="token punctuation">,</span> <span class="token class-name type-declaration">Notification</span> <span class="token variable">$notification</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token variable">$message</span> <span class="token operator">=</span> <span class="token variable">$notification</span><span class="token operator">-></span><span class="token function">toVoice</span><span class="token punctuation">(</span><span class="token variable">$notifiable</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">        <span class="token comment">// 将通知发送给 $notifiable 实例</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>一旦定义了通知频道类，你应该在所有通知里通过 <code v-pre>via</code> 方法来简单地返回这个频道的类名。</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">namespace</span> <span class="token package">App<span class="token punctuation">\</span>Notifications</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Bus<span class="token punctuation">\</span>Queueable</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Channels<span class="token punctuation">\</span>VoiceChannel</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Channels<span class="token punctuation">\</span>Messages<span class="token punctuation">\</span>VoiceMessage</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Notifications<span class="token punctuation">\</span>Notification</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Contracts<span class="token punctuation">\</span>Queue<span class="token punctuation">\</span>ShouldQueue</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">InvoicePaid</span> <span class="token keyword">extends</span> <span class="token class-name">Notification</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">use</span> <span class="token package">Queueable</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * 获取通知频道</span>
<span class="line">     *</span>
<span class="line">     * <span class="token keyword">@param</span>  <span class="token class-name"><span class="token keyword">mixed</span></span>  <span class="token parameter">$notifiable</span></span>
<span class="line">     * <span class="token keyword">@return</span> <span class="token class-name"><span class="token keyword">array</span><span class="token punctuation">|</span><span class="token keyword">string</span></span></span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">via</span><span class="token punctuation">(</span><span class="token variable">$notifiable</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token punctuation">[</span><span class="token class-name static-context">VoiceChannel</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * 获取通知的声音展示方式</span>
<span class="line">     *</span>
<span class="line">     * <span class="token keyword">@param</span>  <span class="token class-name"><span class="token keyword">mixed</span></span>  <span class="token parameter">$notifiable</span></span>
<span class="line">     * <span class="token keyword">@return</span> <span class="token class-name">VoiceMessage</span></span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">toVoice</span><span class="token punctuation">(</span><span class="token variable">$notifiable</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token comment">// ...</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="译者署名" tabindex="-1"><a class="header-anchor" href="#译者署名"><span>译者署名</span></a></h2>
<table>
<thead>
<tr>
<th>用户名</th>
<th>头像</th>
<th>职能</th>
<th>签名</th>
</tr>
</thead>
<tbody>
<tr>
<td><a href="https://learnku.com/users/7283" target="_blank" rel="noopener noreferrer">@carlclone</a></td>
<td><img src="https://cdn.learnku.com/uploads/avatars/7283_1481302280.jpeg?imageView2/1/w/200/h/200" alt="200"></td>
<td>翻译</td>
<td><a href="https://github.com/carlclone" target="_blank" rel="noopener noreferrer">@carlclone</a> at Github</td>
</tr>
</tbody>
</table>
<hr>
<blockquote>
<p>{note} 欢迎任何形式的转载，但请务必注明出处，尊重他人劳动共创开源社区。</p>
<p>转载请注明：本文档由 Laravel China 社区 <a href="https://laravel-china.org/" target="_blank" rel="noopener noreferrer">laravel-china.org</a> 组织翻译，详见 <a href="https://learnku.com/laravel/t/5756/laravel-55-document-translation-call-come-and-join-the-translation" target="_blank" rel="noopener noreferrer">翻译召集帖</a>。</p>
<p>文档永久地址： <a href="https://learnku.com/docs/laravel" target="_blank" rel="noopener noreferrer">《Laravel 中文文档》</a></p>
</blockquote>
</div></template>


